package com.elvin.platform.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.elvin.platform.beans.Dictionary;
import com.elvin.platform.beans.Menu;
import com.elvin.platform.service.MenuService;
import com.elvin.platform.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
@RequestMapping("/menuController")
@Log4j2
@Api("菜单管理")
public class MenuController {
    @Resource
    private MenuService service;
    @ApiIgnore
    @ApiOperation(value = "获取所有菜单")
    @PostMapping("/queryAll")
    public Result<Object> queryAll() {
        Result<Object> result = new Result<>();
        try {
            List<Menu> selectList = service.selectList(null);
            result.success(selectList);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页获取所有数据")
    @PostMapping("/selectPage")
    public Result<Object> selectForPage(
            @ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false)Integer pageNumber,
            @ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false)Integer pageSize
    ) {
        Result<Object> result = new Result<>();
        try {
          int count = service.selectCount(null);
          if (count > 0) {
            List<Menu> selectList = new ArrayList<>();
            if (pageNumber != null && pageSize != null) {
              Page<Menu> page = new Page<>(pageNumber, pageSize);
              service.selectPage(page, null);
              selectList = page.getRecords();
            } else {
              selectList = service.selectList(null);
            }
            result.success(selectList,count);
          }else {
            result.success(null,count);
          }

        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("/insert")
    public Result<Object> insert(@RequestBody Menu entity) {
        Result<Object> result = new Result<>();
        try {
            service.insert(entity);
            result.success(entity, "新增成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping("/update")
    public Result<Object> update(@RequestBody Menu entity) {
        Result<Object> result = new Result<>();
        try {
            service.updateById(entity);
            result.success("修改成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除菜单")
    @PostMapping("/delete")
    public Result<Object> delete(@ApiParam(name = "id",value = "菜单id",required = true) @RequestParam("id") Integer id) {
        Result<Object> result = new Result<>();
        try {
            service.deleteById(id);
            result.success("删除成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据ID获取菜单")
    @PostMapping("/getById")
    public Result<Object> getById(@ApiParam(name = "id",value = "菜单id",required = true) @RequestParam("id") Integer id) {
        Result<Object> result = new Result<>();
        try {
            Menu entity = new Menu();
            entity.setId(id);
            entity = service.selectById(entity);
            result.success(entity);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }
}

